//
//  ShoppingCartModel.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/9.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import SwiftyJSON
struct CommodityModel {
    /// banners
    var bannsersUrl: [String] = []
    /// 品牌名称
    var brandname = ""
    /// 具体参数
    var parameter = "这是商品参数，参数"
    /// 价格
    var price = ""
    /// 原始价格
    var originalPrice = "999"
    /// 库存数量
    var inventory = ""
    /// 规格
    var specifications: [String] = []
    /// 商品描述
    var describe = ""
    /// 商品图片
    var picturesUrl: [String] = []
    /// 好评率
    var praiseRate = ""
    /// 评价数量
    var evaluationNumber = ""
    /// 所有评论
    var commodityCommentModel: [CommodityCommentModel] = []
}

extension CommodityModel {
    init(_ json: [String: Any]) {
        let jsonData = JSON(json).dictionaryValue
        
        brandname = jsonData["name"]?.string ?? ""
        price = jsonData["price"]?.string ?? "0"
        
        let tempBanners = jsonData["bannerImages"]?.array
        guard let banners = tempBanners else {return}
        for banner in banners {
            let banUrl = banner.string ?? ""
            bannsersUrl.append(banUrl)
        }
        
        let tempDetailImages = jsonData["detalImages"]?.array
        guard let detailImages = tempDetailImages else { return }
        for commodityImage in detailImages {
            let imageUrl = commodityImage.string ?? ""
            picturesUrl.append(imageUrl)
        }
        
        let comments = jsonData["comments"]?.array
        for comment in comments! {
            let comDic = comment.dictionary
            var comData = CommodityCommentModel()
            comData.userInfoModel.name = comDic!["name"]?.string ?? ""
            comData.commentContent = comDic!["content"]?.string ?? ""
            self.commodityCommentModel.append(comData)
        }
    }
}
